package org.zht.dormpro.auth.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.zht.dormpro.auth.bean.PageBean;
import org.zht.dormpro.auth.model.SysRole;
import org.zht.dormpro.auth.service.IRoleService;
import org.zht.dormpro.pub.ResponseMap;

import java.util.*;

@Controller
@RequestMapping("/auth/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @RequestMapping("/rolePage")
    public String rolePage() {
        return "/role/role_list";
    }

    @RequestMapping("/roleAddPage")
    public String roleAddPage() {
        return "/role/role_add";
    }

    @RequestMapping("/roleEditPage/{roleId}")
    public String roleEditPage(@PathVariable("roleId") Integer roleId, Model model) {
        SysRole sysRole = roleService.getRoleById(roleId);
        model.addAttribute("role", sysRole);
        return "/role/role_edit";
    }
    @RequestMapping(value = "/editRole",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap editRole(String[] menuIds, SysRole sysRole) {
        //更新角色
        boolean success = roleService.updateRole(sysRole);
        if(success){
            //更新角色和权限
            boolean b = roleService.updateRolePermission(new ArrayList<String>(Arrays.asList(menuIds)), sysRole.getId());
            if(b){
               return ResponseMap.success("角色更新成功!");
            }
        }
        return ResponseMap.error("角色更新失败!");
    }

    @RequestMapping(value = "/addRole", method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap addRole(String[] menuIds, SysRole sysRole) {
        Integer roleId = roleService.addRole(sysRole);
        if (roleId > 0) {
            if (menuIds != null && menuIds.length > 0) {
                //z注意arrays.asList克隆对象问题,不支持removeadd
                boolean success = roleService.addRolePermission(new ArrayList<String>(Arrays.asList(menuIds)), roleId);
                if (success) {
                    return ResponseMap.success("角色添加成功");
                }
            }
            return ResponseMap.success("角色添加成功");
        }
        return ResponseMap.error("角色添加失败");
    }

    @RequestMapping(value = "/deleteRole", method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap deleteRole(Integer id) {
        boolean success = roleService.deleteRole(id);
        if (success) {
            return ResponseMap.success("角色删除成功!");
        }
        return ResponseMap.error("角色删除失败!");
    }

    @RequestMapping(value = "/batchRemoveRole", method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap batchRemoveRole(@RequestParam("roleIds[]") String[] roleIds) {
        if (roleIds != null) {
            boolean success = roleService.batchRemoveRole(new ArrayList<String>(Arrays.asList(roleIds)));
            if (success) {
                return ResponseMap.success("角色删除成功!");
            }
        }
        return ResponseMap.error("角色删除失败!");
    }

    @ResponseBody
    @RequiresPermissions("auth:list:all")
    @RequestMapping("/listRole")
    public ResponseMap listRole(@RequestParam Map<String, Object> params) {
        PageBean pageBean = new PageBean(params);
        List<SysRole> roles = roleService.getRoleByCondition(pageBean);
        Map<String, Object> attr = new HashMap<>();
        attr.put("total", pageBean.getTotal());
        return ResponseMap.success(roles, attr);
    }

}
